package com.interview.javabase.algorithm.链表;

import java.util.Stack;

/**
 * @author zhangshguang
 * @date 2020/10/12
 */
public class 回文链表 {

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode node1 = new ListNode(2);
        ListNode node2 = new ListNode(3);
        ListNode node3 = new ListNode(2);
        ListNode node4 = new ListNode(1);
        head.next = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;

        boolean test = test(head);
        System.out.println(test);
    }

    public static boolean test(ListNode head) {
        if (head == null) {
            return false;
        }

        Stack<Integer> stack = new Stack<>();

        ListNode temp = head;

        while (temp != null) {

            stack.push(temp.val);
            temp = temp.next;
        }

        temp = head;
        while (temp != null) {

            if (!stack.pop().equals(temp.val)) {
                return false;
            }

            temp = temp.next;
        }

        return true;
    }
}
